package com.lucun.es_transaction.service.impl;

import com.lucun.es_transaction.entity.UserCommodity;
import com.lucun.es_transaction.entity.UserLog;
import com.lucun.es_transaction.mapper.UserCommodityMapper;
import com.lucun.es_transaction.mapper.UserLogMapper;
import com.lucun.es_transaction.service.UserCommService;
import com.lucun.es_transaction.util.BaseException;
import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class UserCommServiceImpl implements UserCommService {
    @Autowired
    UserLogMapper userLogMapper;
    @Autowired
    UserCommodityMapper  userCommodityMapper;
//    添加
    @Override
    public JsonResult<?> index(UserCommodity userCommodity, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(userCommodity)){
            throw new BaseException("添加数据为空");
        }
        int token = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(token)){
            throw new BaseException("解析token失败");
        }
        int uid = userCommodity.getUserId();
        if (ObjectUtils.isEmpty(uid)){
            throw new BaseException("添加用户id不能为空");
        }
        int commid = userCommodity.getCommId();
        if (ObjectUtils.isEmpty(commid)){
            throw new BaseException("添加商品id不能为空");
        }
        userCommodity.setCreateTime(new Date());
        int i = userCommodityMapper.insert(userCommodity);
        UserLog userLog = new UserLog();
        userLog.setUserId(token);
        userLog.setLogName(token+"用户收藏商品成功");
        userLog.setLogType((byte)1);
        userLog.setCreateTime(new Date());
        if (i>0){
            userLog.setLogStatus((byte)1);
            userLogMapper.insert(userLog);
            return new JsonResult<>("200","添加成功",userCommodity);
        }else{
            userLog.setLogStatus((byte)0);
            userLog.setLogName(token+"用户收藏商品失败");
            userLogMapper.insert(userLog);
            return new JsonResult<>("500","添加失败",userCommodity);
        }
    }
//删除（取消收藏）
    @RequestMapping("/delete")
    @Override
    public JsonResult<?> delete(int id,HttpServletRequest r) {
        if (ObjectUtils.isEmpty(id)){
            throw new BaseException("没有获取这个id");
        }
        int token = JwtUtil.getIdByToken(r);
        if (ObjectUtils.isEmpty(token)){
            throw new BaseException("解析token失败");
        }
        int i = userCommodityMapper.deleteByPrimaryKey(id);
        UserLog userLog = new UserLog();
        userLog.setUserId(token);
        userLog.setLogName(token+"用户取消收藏此商品成功");
        userLog.setLogType((byte)1);
        userLog.setCreateTime(new Date());
        if (i>0){
            userLog.setLogStatus((byte)1);
            userLogMapper.insert(userLog);
            return new JsonResult<>("200","删除成功",id);
        }else{
            userLog.setLogStatus((byte)0);
            userLog.setLogName(token+"用户取消收藏此商品失败");
            userLogMapper.insert(userLog);
            return new JsonResult<>("500","删除失败",id);
        }
    }
}
